<?php

/**
 * 代理层级模型
 * Class Agent
 */
namespace App\Models;

Class Agentlev extends BaseModel
{

    protected $table = "agent_lev";

    /**
     * @param $where
     * 获取管理员信息
     */
    public function getOne($where, $fileds = '*')
    {
        return $this->_getOne($where, $fileds);
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where, $data)
    {
        return $this->_updata($where, $data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($id)
    {
        return $this->_del(['id' => $id]);
    }

    public function getAgentLev($condtion)
    {
        $data = $this->getOne($condtion);
        if ($data) {
            $data = $data->toArray();
        }
        return $data;
    }

    /**
     * 获取所有代理层级信息
     * 并分页显示
     */
    public function getAgentList($request)
    {
        $where = [];
        $where['bid'] = session('adminId');
        //dd(session('adminId'));
        $order['id'] = "desc";
        $rows = $this->getList($where, '*', $order);
        if ($rows) {
            $rows = $rows->toArray();
        }
        return $rows;
    }

    /**
     * 编辑代理层级
     */
    public function getEdit($request)
    {
        $id = $request['id'];
        if (empty($id)) {
            $res = ['status' => 0, 'msg' => "参数为空"];
            return $res;
        }
        $where['id'] = $id;
        $data = $this->getOne($where);
        if (empty($data)) {
            $res = ['status' => 0, 'msg' => "没有记录"];
            return $res;
        }
        $res = ['status' => 200, 'msg' => "ok", 'data' => $data];
        return $res;
    }

    /**
     * 编辑代理层级信息
     */
    public function saveAgentLev($request)
    {
        $id = trim($request['id']);
        $lev = trim($request['lev']);
        $agent_lev_name = trim($request['lev_name']);
        $discount = trim($request['discount']);
        if (empty($lev) || empty($agent_lev_name) || empty($discount)) {
            $res = ['status' => 0, 'msg' => "代理层级信息填写不完整"];
            return $res;
        }

        if (!is_numeric($discount) && $discount >= 0 && $discount <= 100) {
            $res = ['status' => 0, 'msg' => "折扣必须为0-100之间的正整数"];
            return $res;
        }

        $data['lev'] = $lev;
        $data['lev_name'] = $agent_lev_name;
        $data['discount'] = $discount;
        $data['updated_at'] = date('Y-m-d H:i:s', time());
        $condition['id'] = $request['id'];
        $data['bid'] = session('adminId');

        $oneinfo = $this->getOne(['id'=>$id]);     //求当前选中层级信息
        if($oneinfo['lev'] != $lev){               //如果级别发生改变就做判断

            $check_lev = $this->getOne(['lev'=>$lev,'bid'=>$data['bid']]);
                if($check_lev){
                    return ['status'=>0,'msg'=>'只能有一个'.$lev.'级'];
                }

        }

        $resulte = $this->up($condition, $data);
        if ($resulte) {
            $res = ['status' => 200, 'msg' => "代理层级信息修改成功"];
            return $res;
        }
        $res = ['status' => 0, 'msg' => "代理层级信息修改失败"];
        return $res;
    }

    /**
     * 添加代理层级信息
     */
    public function addAgentLev($request)
    {
        $lev = trim($request['lev']);
        $agent_lev_name = trim($request['lev_name']);
        $discount = trim($request['discount']);
        if (empty($lev) || empty($agent_lev_name) || empty($discount)) {
            $res = ['status' => 0, 'msg' => "代理层级信息填写不完整"];
            return $res;
        }
        if (!is_numeric($discount) && $discount >= 0 && $discount <= 100) {
            $res = ['status' => 0, 'msg' => "折扣必须为0-100之间的正整数"];
            return $res;
        }

        $data['lev'] = $lev;
        $data['lev_name'] = $agent_lev_name;
        $data['bid'] = session('adminId');
        $data['discount'] = $discount;
        $data['created_at'] = date("Y-m-d", time());
        $data['updated_at'] = date('Y-m-d H:i:s', time());

        $check_lev = $this->getOne(['lev'=>$lev,'bid'=>$data['bid']]);
        if($check_lev){
            return ['status'=>0,'msg'=>'只能有一个'.$lev.'级'];
        }

        $resulte = $this->add($data);
        if ($resulte) {
            $res = ['status' => 200, 'msg' => "代理层级信息添加成功"];
            return $res;
        }
        $res = ['status' => 0, 'msg' => "代理层级信息添加失败"];
        return $res;
    }

    /**
     *
     * 删除代理层级
     */

    public function delAgentLev($request)
    {
        //dd($request['id']);
        $where['id'] = $request['id'];
        $did = $this->del($request['id']);
        if(!$did){
            return ['status'=>0,'msg'=>'删除层级失败！请重试'];
        }

        return ['status'=>1,'msg'=>'删除代理层级成功！'];

    }

}